<?php

namespace app\admin\controller\wu;

use app\common\controller\Backend;
use think\Db;
use think\Session;

/**
 * Wu制程計劃管理
 *
 * @icon fa fa-circle-o
 */
class Planlog extends Backend
{

    /**
     * Planlog模型对象
     * @var \app\admin\model\wu\Planlog
     */
    protected $model = null;

    protected $noNeedRight = ['tzrl', 'daka', 'detail2', 'detail3', 'edit2', 'detail_forworkorder', 'ygyg', 'multi', 'xckb', 'editpdday', 'pcak', 'detail', 'subprocess', 'mainprocess', 'addsubprocess', 'addmainprocess', 'index1', 'index2', 'dopython', 'znpd', 'znpdlist', 'gantt', 'gantt_w', 'plcz', 'gantt_z', 'gantt_z1', 'gstj', 'gstj0'];

    protected $noNeedLogin = ['detail3', 'detail_forworkorder'];

    private $procedurelist = null;
    private $manlist = null;
    private $switch = 0;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wu\Planlog;
        $this->view->assign("stateList", $this->model->getStateList());

        $this->procedurelist = collection((new \app\admin\model\wu\Procedure)->field('id, name')->select())->toArray();
        //man替换为机台
//        $this->manlist = collection((new \app\admin\model\wu\Man)->field('id, name')->select())->toArray();
        $this->manlist = collection((new \app\admin\model\wu\Jt)->field('id, name')->select())->toArray();

        $this->switch = Session::get(self::APS_PLANLOG_SWITCH);
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['workorder'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['workorder'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            $list = $this->setnameforlist($list);

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        if ($this->switch == 0) {
            $switchname = "排程全开";
        } else {
            $switchname = "排程全关";
        }
        $this->view->assign("switchname", $switchname);

        $pdday = date("Y-m-d", time());
        $vk = \app\admin\model\wu\Vk::get(1);
        if ($vk) {
            $pdday = $vk->v;
        }

        $this->view->assign("pdday", $pdday);

        return $this->view->fetch();
    }

    function setnameforlist($list)
    {
        foreach ($list as &$row) {
            $row['procedure1_name'] = $this->getprocedure_name($row['procedure1_id']);
            $row['procedure1_man_name'] = $this->getman_name($row['procedure1_man_id']);
            $row['procedure2_name'] = $this->getprocedure_name($row['procedure2_id']);
            $row['procedure2_man_name'] = $this->getman_name($row['procedure2_man_id']);
            $row['procedure3_name'] = $this->getprocedure_name($row['procedure3_id']);
            $row['procedure3_man_name'] = $this->getman_name($row['procedure3_man_id']);
            $row['procedure4_name'] = $this->getprocedure_name($row['procedure4_id']);
            $row['procedure4_man_name'] = $this->getman_name($row['procedure4_man_id']);
            $row['procedure5_name'] = $this->getprocedure_name($row['procedure5_id']);
            $row['procedure5_man_name'] = $this->getman_name($row['procedure5_man_id']);
            $row['procedure6_name'] = $this->getprocedure_name($row['procedure6_id']);
            $row['procedure6_man_name'] = $this->getman_name($row['procedure6_man_id']);
            $row['procedure7_name'] = $this->getprocedure_name($row['procedure7_id']);
            $row['procedure7_man_name'] = $this->getman_name($row['procedure7_man_id']);
            $row['procedure8_name'] = $this->getprocedure_name($row['procedure8_id']);
            $row['procedure8_man_name'] = $this->getman_name($row['procedure8_man_id']);
            $row['procedure9_name'] = $this->getprocedure_name($row['procedure9_id']);
            $row['procedure9_man_name'] = $this->getman_name($row['procedure9_man_id']);
            $row['procedure10_name'] = $this->getprocedure_name($row['procedure10_id']);
            $row['procedure10_man_name'] = $this->getman_name($row['procedure10_man_id']);
            $row['procedure11_name'] = $this->getprocedure_name($row['procedure11_id']);
            $row['procedure11_man_name'] = $this->getman_name($row['procedure11_man_id']);
            $row['procedure12_name'] = $this->getprocedure_name($row['procedure12_id']);
            $row['procedure12_man_name'] = $this->getman_name($row['procedure12_man_id']);
        }

        return $list;
    }

    /**
     * 逾期任务打卡
     */
    public function index1()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $yesterday = date("Y-m-d", strtotime("-1 day"));
//            $yesterday = date('2019-05-06');
            //应该是昨天完成的，今天未完成

            $where1 = "(
                (finishnum = 0 and Date(procedure1_finishdate) <= '{$yesterday}' and procedure1_startdate > '0000-00-00 00:00:00')
                or (finishnum = 1 and Date(procedure2_finishdate) <= '{$yesterday}' and procedure2_startdate > '0000-00-00 00:00:00')
                or (finishnum = 2 and Date(procedure3_finishdate) <= '{$yesterday}' and procedure3_startdate > '0000-00-00 00:00:00')
                or (finishnum = 3 and Date(procedure4_finishdate) <= '{$yesterday}' and procedure4_startdate > '0000-00-00 00:00:00')
                or (finishnum = 4 and Date(procedure5_finishdate) <= '{$yesterday}' and procedure5_startdate > '0000-00-00 00:00:00')
                or (finishnum = 5 and Date(procedure6_finishdate) <= '{$yesterday}' and procedure6_startdate > '0000-00-00 00:00:00')
                or (finishnum = 6 and Date(procedure7_finishdate) <= '{$yesterday}' and procedure7_startdate > '0000-00-00 00:00:00')
                or (finishnum = 7 and Date(procedure8_finishdate) <= '{$yesterday}' and procedure8_startdate > '0000-00-00 00:00:00')
                or (finishnum = 8 and Date(procedure9_finishdate) <= '{$yesterday}' and procedure9_startdate > '0000-00-00 00:00:00')
                or (finishnum = 9 and Date(procedure10_finishdate) <= '{$yesterday}' and procedure10_startdate > '0000-00-00 00:00:00')
                or (finishnum = 10 and Date(procedure11_finishdate) <= '{$yesterday}' and procedure11_startdate > '0000-00-00 00:00:00')
                or (finishnum = 11 and Date(procedure12_finishdate) <= '{$yesterday}' and procedure12_startdate > '0000-00-00 00:00:00')
            )";


            $total = $this->model
                ->with(['workorder'])
                ->where($where)
                ->where($where1)
                ->where('finishnum < maxnum')
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['workorder'])
                ->where($where)
                ->where($where1)
                ->where('finishnum < maxnum')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $list = $this->setnameforlist($list);

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 今日任务 只列工序， 不分页了
     */
    public function index2()
    {

        if ($this->request->isAjax()) {
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);

            $offset = $this->request->get("offset", 0);
            $limit = $this->request->get("limit", 0);

            $now = $filter['today'];
            $nowA = strtotime($now);

            $tomorrowA = $nowA + 24 * 3600;
            $tomorrow = date("Y-m-d", $tomorrowA);

            $where = "maxnum > finishnum and switch = 1 and (
                (Date(procedure1_startdate) <= '{$now}' and Date(procedure1_finishdate) >= '{$now}')
                or (Date(procedure2_startdate) <= '{$now}' and Date(procedure2_finishdate) >= '{$now}')
                or (Date(procedure3_startdate) <= '{$now}' and Date(procedure3_finishdate) >= '{$now}')
                or (Date(procedure4_startdate) <= '{$now}' and Date(procedure4_finishdate) >= '{$now}')
                or (Date(procedure5_startdate) <= '{$now}' and Date(procedure5_finishdate) >= '{$now}')
                or (Date(procedure6_startdate) <= '{$now}' and Date(procedure6_finishdate) >= '{$now}')
                or (Date(procedure7_startdate) <= '{$now}' and Date(procedure7_finishdate) >= '{$now}')
                or (Date(procedure8_startdate) <= '{$now}' and Date(procedure8_finishdate) >= '{$now}')
                or (Date(procedure9_startdate) <= '{$now}' and Date(procedure9_finishdate) >= '{$now}')
                or (Date(procedure10_startdate) <= '{$now}' and Date(procedure10_finishdate) >= '{$now}')
                or (Date(procedure11_startdate) <= '{$now}' and Date(procedure11_finishdate) >= '{$now}')
                or (Date(procedure12_startdate) <= '{$now}' and Date(procedure12_finishdate) >= '{$now}')
            )";

            $list = $this->model
                ->with(['workorder'])
                ->where($where)
                ->select();

            $list = collection($list)->toArray();
            $list2 = []; //今日需要做的制程
            $total = 0;
            foreach ($list as $row) {
                $row = $this->addArrayinfo($row);

                $start = $row['finishnum'];
                $end = $row['maxnum'];
                for ($i = 0; $i < $end; $i++) {
                    if ($row['start_times'][$i] > $tomorrowA || $row['end_times'][$i] < $nowA) {
                        continue;
                    }

                    $zt = "未完工";
                    $procedurestate = \app\admin\model\wu\Procedurestate::where("planlog_id", $row['id'])
                        ->where("procedure_id", $row['procedures'][$i]['id'])->find();
                    if ($procedurestate) {
                        $state = intval($procedurestate->state);
                        if ($state == 0) {
                            $zt = "待施工";
                        } else if ($state == 1) {
                            $zt = "工作中";
                        } else {
                            $zt = "完工";
                        }
                    }

                    if ($total >= $offset && $total < $offset + $limit) {
                        $list2[] = array(
                            'id' => $procedurestate->id,
                            'man_name' => $row['procedure_mans'][$i],
                            'procedure_name' => $row['procedure_names'][$i],
                            'num' => $row['workorder']['num'],
                            'man_num' => $row['procedures'][$i]['mannum'],
                            'startdate' => date("Y-m-d H:i", $row['start_times'][$i]),
                            'workorder_name' => $row['workorder']['name'],
                            'manhours' => $row['procedures'][$i]['manhours'],
                            'MainProcessName' => $row['workorder']['ProductName'],
                            'ProductNumber' => $row['workorder']['ProductName'],
                            'ProductName' =>$row['workorder']['ProductName'],
                            'workorder_id' => $row['workorder_id'],
                            'zt' => $zt,
                        );
                    }
                    $total++;
                }
            }

            $yg = \app\admin\model\wu\Ygyg::where('date', $now)->find();
            $jrgs = 0;
            $mannum = 0;
            if ($yg) {
                $jrgs = intval($yg->sumAllManhours * $yg->gsb / 100);
                $mannum = $yg->sumMansPerDay;
            }

            $result = array("total" => $total, "rows" => $list2, "extend" => ['jrgs' => $jrgs, 'mannum' => $mannum]);

            return json($result);
        }
        return $this->view->fetch();
    }

    function getprocedure_name($procedure_id)
    {
        if ($procedure_id > 0 && $this->procedurelist) {
            foreach ($this->procedurelist as $row) {
                if ($row['id'] == $procedure_id) {
                    return $row['name'];
                }
            }
        }

        return '';
    }

    //获取机台名称
    function getman_name($man_id)
    {
        if ($man_id > 0 && $this->manlist) {
            foreach ($this->manlist as $row) {
                if ($row['id'] == $man_id) {
                    return $row['name'];
                }
            }
        }

        return '';
    }


    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    //是否添加子制程（产品）
                    if (intval($params['subprocess_id']) == 0) {
                        $spmodel = new \app\admin\model\wu\Subprocess;
                        $sp = $spmodel->where('subname', $params['subprocess'])->find();
                        if (!$sp) {
                            $params['subname'] = $params['subprocess'];
                            $spmodel->allowField(true)->save($params);
                        }
                    }

                    //重整工序
                    $params = $this->resetprocedure($params);

                    //加入工单
                    $womodel = new \app\admin\model\wu\Workorder;

                    $params['name'] = $params['subprocess'] . '_' . date('YmdHis'); //工单名称=子制程名称+时间
                    $params['startdate'] = date('Y-m-d H:i:s');

                    $womodel->allowField(true)->save($params);
                    $params['workorder_id'] = $womodel->id;

                    //加入planlog
                    $params['workorder_finishdate'] = $params['finishdate'];
                    $result = $this->model->allowField(true)->save($params);

                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $html = '';
        for ($i = 1; $i < 13; $i++) {
            $html .= '<div class="form-group">';
            $html .= '<label class="control-label col-xs-12 col-sm-2">工序' . $i . '名称:</label>';
            $html .= '<div class="col-xs-12 col-sm-2">';
            $html .= '<input id="c-procedure' . $i . '"  disabled="disabled" class="form-control" name="row[procedure' . $i . ']" type="text" value="">';
            $html .= '</div>';
            $html .= '<label class="control-label col-xs-12 col-sm-2">工序' . $i . '工时:</label>';
            $html .= '<div class="col-xs-12 col-sm-2">';
            $html .= '<input id="c-procedure' . $i . '_manhours"  class="form-control" step="0.01" name="row[procedure' . $i . '_manhours]" type="number" value="">';
            $html .= '</div>';
            $html .= '<a id="b-procedure' . $i . '" class="btn btn-info" href="javascript:;" > 移除 </a>';
            $html .= '</div>';
        }

        $this->view->assign("html", $html);

        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    if (isset($params['workorder_finishdate'])) {
                        if ($row->finishnum >= $row->maxnum) {
                            $this->error('工单已完成不能修改计划完工时间！');
                        }
                        $result = $row->allowField(true)->save($params);
                        $this->success();
                    }
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    if ($params['finishnum'] >= $row->maxnum) {
                        $params['finishnum'] = $row->maxnum;
                        $params['sjwg'] = date('Y-m-d H:i:s');
                        $params['state'] = '已完成';
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);

        $startdate = [];
        $startdate[] = $row['procedure1_startdate'];
        $startdate[] = $row['procedure2_startdate'];
        $startdate[] = $row['procedure3_startdate'];
        $startdate[] = $row['procedure4_startdate'];
        $startdate[] = $row['procedure5_startdate'];
        $startdate[] = $row['procedure6_startdate'];
        $startdate[] = $row['procedure7_startdate'];
        $startdate[] = $row['procedure8_startdate'];
        $startdate[] = $row['procedure9_startdate'];
        $startdate[] = $row['procedure10_startdate'];
        $startdate[] = $row['procedure11_startdate'];
        $startdate[] = $row['procedure12_startdate'];
        $finishdate = [];
        $finishdate[] = $row['procedure1_finishdate'];
        $finishdate[] = $row['procedure2_finishdate'];
        $finishdate[] = $row['procedure3_finishdate'];
        $finishdate[] = $row['procedure4_finishdate'];
        $finishdate[] = $row['procedure5_finishdate'];
        $finishdate[] = $row['procedure6_finishdate'];
        $finishdate[] = $row['procedure7_finishdate'];
        $finishdate[] = $row['procedure8_finishdate'];
        $finishdate[] = $row['procedure9_finishdate'];
        $finishdate[] = $row['procedure10_finishdate'];
        $finishdate[] = $row['procedure11_finishdate'];
        $finishdate[] = $row['procedure12_finishdate'];


        $html = '0000-00-00 00:00:00';
        for ($i = 1; $i <= $row['maxnum']; $i++) {
            $html .= '<div class="form-group">';
            $html .= '<label class="control-label col-xs-12 col-sm-2">工序' . $i . '开工日期:</label>';
            $html .= '<div class="col-xs-12 col-sm-3">';
            $html .= '<input id="c-procedure' . $i . '_startdate" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[procedure' . $i . '_startdate]" type="text" value="' . $startdate[$i - 1] . '">';
            $html .= '</div>';
            $html .= '<label class="control-label col-xs-12 col-sm-2">工序计划' . $i . '完工日期:</label>';
            $html .= '<div class="col-xs-12 col-sm-3">';
            $html .= '<input id="c-procedure' . $i . '_finishdate" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[procedure' . $i . '_finishdate]" type="text" value="' . $finishdate[$i - 1] . '">';
            $html .= '</div>';
            $html .= '<a data-id="' . ($i - 1) . '" class="btn btn-info btn-proclear" href="javascript:;" > 清空 </a>';
            $html .= '</div>';
        }


        $this->view->assign("html", $html);
        return $this->view->fetch();
    }

    public function edit2($ids = NULL)
    {
        $planlog_ids = Session::get("planlog_ids");

        $row = $this->model->get($planlog_ids);
        if (!$row)
            $this->error(__('No Results were found'));

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    if (isset($params['procedure_mannum2'])) {
                        $index = intval($ids);
                        switch ($index) {
                            case 1:
                                $params['procedure1_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 2:
                                $params['procedure2_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 3:
                                $params['procedure3_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 4:
                                $params['procedure4_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 5:
                                $params['procedure5_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 6:
                                $params['procedure6_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 7:
                                $params['procedure7_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 8:
                                $params['procedure8_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 9:
                                $params['procedure9_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 10:
                                $params['procedure10_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 11:
                                $params['procedure11_mannum'] = $params['procedure_mannum2'];
                                break;
                            case 12:
                                $params['procedure12_mannum'] = $params['procedure_mannum2'];
                                break;
                            default:
                                $this->error(__('No Results were found'));
                                break;

                        }
                        $result = $row->allowField(true)->save($params);
                        $this->success();
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
    }

    public function mainprocess()
    {

        if ($this->request->isAjax()) {

            $mpmodel = new \app\admin\model\wu\Mainprocess;
            $prmodel = new \app\admin\model\wu\Procedure;

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $mpmodel
                ->where($where)
                ->where('state', 1)
                ->count();

            $mplist = $mpmodel
                ->where($where)
                ->where('state', 1)
                ->field('id, name, manhours')
                ->limit($offset, $limit)
                ->select();

            $list = [];
            foreach ($mplist as $row) {
                $prlist = collection($prmodel
                    ->where('mainprocess_id', $row['id'])
                    ->field('id, name, manhours')
                    ->select())->toArray();

                $list[] = $this->rewritepinfo($prlist, $row);
            }

            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }

        return $this->view->fetch();
    }

    function rewritepinfo($prlist, $row)
    {
        if (!$prlist || !$row) {
            return $row;
        }
        $row['procedure1_name'] = '0000-00-00 00:00:00';
        $row['procedure1_manhours'] = '0000-00-00 00:00:00';
        $row['procedure2_name'] = '0000-00-00 00:00:00';
        $row['procedure2_manhours'] = '0000-00-00 00:00:00';
        $row['procedure3_name'] = '0000-00-00 00:00:00';
        $row['procedure3_manhours'] = '0000-00-00 00:00:00';
        $row['procedure4_name'] = '0000-00-00 00:00:00';
        $row['procedure4_manhours'] = '0000-00-00 00:00:00';
        $row['procedure5_name'] = '0000-00-00 00:00:00';
        $row['procedure5_manhours'] = '0000-00-00 00:00:00';
        $row['procedure6_name'] = '0000-00-00 00:00:00';
        $row['procedure6_manhours'] = '0000-00-00 00:00:00';
        $row['procedure7_name'] = '0000-00-00 00:00:00';
        $row['procedure7_manhours'] = '0000-00-00 00:00:00';
        $row['procedure8_name'] = '0000-00-00 00:00:00';
        $row['procedure8_manhours'] = '0000-00-00 00:00:00';
        $row['procedure9_name'] = '0000-00-00 00:00:00';
        $row['procedure9_manhours'] = '0000-00-00 00:00:00';
        $row['procedure10_name'] = '0000-00-00 00:00:00';
        $row['procedure10_manhours'] = '0000-00-00 00:00:00';
        $row['procedure11_name'] = '0000-00-00 00:00:00';
        $row['procedure11_manhours'] = '0000-00-00 00:00:00';
        $row['procedure12_name'] = '0000-00-00 00:00:00';
        $row['procedure12_manhours'] = '0000-00-00 00:00:00';
        $i = 0;
        foreach ($prlist as $pr) {
            switch ($i) {
                case 0:
                    $row['procedure1_id'] = $pr['id'];
                    $row['procedure1_name'] = $pr['name'];
                    $row['procedure1_manhours'] = $pr['manhours'];
                    break;
                case 1:
                    $row['procedure2_id'] = $pr['id'];
                    $row['procedure2_name'] = $pr['name'];
                    $row['procedure2_manhours'] = $pr['manhours'];
                    break;
                case 2:
                    $row['procedure3_id'] = $pr['id'];
                    $row['procedure3_name'] = $pr['name'];
                    $row['procedure3_manhours'] = $pr['manhours'];
                    break;
                case 3:
                    $row['procedure4_id'] = $pr['id'];
                    $row['procedure4_name'] = $pr['name'];
                    $row['procedure4_manhours'] = $pr['manhours'];
                    break;
                case 4:
                    $row['procedure5_id'] = $pr['id'];
                    $row['procedure5_name'] = $pr['name'];
                    $row['procedure5_manhours'] = $pr['manhours'];
                    break;
                case 5:
                    $row['procedure6_id'] = $pr['id'];
                    $row['procedure6_name'] = $pr['name'];
                    $row['procedure6_manhours'] = $pr['manhours'];
                    break;
                case 6:
                    $row['procedure7_id'] = $pr['id'];
                    $row['procedure7_name'] = $pr['name'];
                    $row['procedure7_manhours'] = $pr['manhours'];
                    break;
                case 7:
                    $row['procedure8_id'] = $pr['id'];
                    $row['procedure8_name'] = $pr['name'];
                    $row['procedure8_manhours'] = $pr['manhours'];
                    break;
                case 8:
                    $row['procedure9_id'] = $pr['id'];
                    $row['procedure9_name'] = $pr['name'];
                    $row['procedure9_manhours'] = $pr['manhours'];
                    break;
                case 9:
                    $row['procedure10_id'] = $pr['id'];
                    $row['procedure10_name'] = $pr['name'];
                    $row['procedure10_manhours'] = $pr['manhours'];
                    break;
                case 10:
                    $row['procedure11_id'] = $pr['id'];
                    $row['procedure11_name'] = $pr['name'];
                    $row['procedure11_manhours'] = $pr['manhours'];
                    break;
                case 11:
                    $row['procedure12_id'] = $pr['id'];
                    $row['procedure12_name'] = $pr['name'];
                    $row['procedure12_manhours'] = $pr['manhours'];
                    break;
                default:
                    break;
            }
            $i++;
        }

        return $row;
    }


    public function subprocess()
    {
        if ($this->request->isAjax()) {

            $spmodel = new \app\admin\model\wu\Subprocess;
            $prmodel = new \app\admin\model\wu\Procedure;

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $spmodel
                ->with(['mainprocess'])
                ->where($where)
                ->count();

            $splist = $spmodel
                ->field('id, mainprocess_id, subname')
                ->with(['mainprocess'])
                ->where($where)
                ->limit($offset, $limit)
                ->select();

            $list = [];
            foreach ($splist as $row) {
                $prlist = collection($prmodel
                    ->where('mainprocess_id', $row['mainprocess_id'])
                    ->field('id, name, manhours')
                    ->select())->toArray();

                $list[] = $this->rewritepinfo($prlist, $row);
            }

            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }

        return $this->view->fetch();
    }

    public function addsubprocess($ids)
    {
        if ($this->request->isPost()) {

            $row = \app\admin\model\wu\Subprocess::get($ids);
            if (!$row)
                $this->error(__('No Results were found'));

            $row->toArray();

            $mp = \app\admin\model\wu\Mainprocess::get($row['mainprocess_id']);
            if ($mp) {
                $row['mainprocess_name'] = $mp['name'];
            }

            $prmodel = new \app\admin\model\wu\Procedure;

            $prlist = collection($prmodel
                ->where('mainprocess_id', $row['mainprocess_id'])
                ->field('id, name, manhours')
                ->select())->toArray();

            $row = $this->rewritepinfo($prlist, $row);

            $this->success('success', null, $row);
        }
        $this->error(__('没有选中子制程'));

    }

    public function addmainprocess($ids)
    {
        if ($this->request->isPost()) {

            $row = \app\admin\model\wu\Mainprocess::get($ids);
            if (!$row)
                $this->error(__('No Results were found'));

            $row->toArray();

            $prmodel = new \app\admin\model\wu\Procedure;

            $prlist = collection($prmodel
                ->where('mainprocess_id', $row['id'])
                ->field('id, name, manhours')
                ->select())->toArray();

            $row = $this->rewritepinfo($prlist, $row);

            $this->success('success', null, $row);
        }
        $this->error(__('没有选中主制程！'));

    }

    function resetprocedure($params)
    {

        if ($params) {
            $procedures = [];

            $procedures[] = array('id' => $params['procedure1_id'], 'manhours' => $params['procedure1_manhours']);
            $procedures[] = array('id' => $params['procedure2_id'], 'manhours' => $params['procedure2_manhours']);
            $procedures[] = array('id' => $params['procedure3_id'], 'manhours' => $params['procedure3_manhours']);
            $procedures[] = array('id' => $params['procedure4_id'], 'manhours' => $params['procedure4_manhours']);
            $procedures[] = array('id' => $params['procedure5_id'], 'manhours' => $params['procedure5_manhours']);
            $procedures[] = array('id' => $params['procedure6_id'], 'manhours' => $params['procedure6_manhours']);
            $procedures[] = array('id' => $params['procedure7_id'], 'manhours' => $params['procedure7_manhours']);
            $procedures[] = array('id' => $params['procedure8_id'], 'manhours' => $params['procedure8_manhours']);
            $procedures[] = array('id' => $params['procedure9_id'], 'manhours' => $params['procedure9_manhours']);
            $procedures[] = array('id' => $params['procedure10_id'], 'manhours' => $params['procedure10_manhours']);
            $procedures[] = array('id' => $params['procedure11_id'], 'manhours' => $params['procedure11_manhours']);
            $procedures[] = array('id' => $params['procedure12_id'], 'manhours' => $params['procedure12_manhours']);

            $maxnum = 0;
            foreach ($procedures as $row) {
                if (intval($row['id']) == 0) {
                    continue;
                }

                switch ($maxnum) {
                    default:
                    case 0:
                        $params['procedure1_id'] = $row['id'];
                        $params['procedure1_manhours'] = $row['manhours'];
                        break;
                    case 1:
                        $params['procedure2_id'] = $row['id'];
                        $params['procedure2_manhours'] = $row['manhours'];
                        break;
                    case 2:
                        $params['procedure3_id'] = $row['id'];
                        $params['procedure3_manhours'] = $row['manhours'];
                        break;
                    case 3:
                        $params['procedure4_id'] = $row['id'];
                        $params['procedure4_manhours'] = $row['manhours'];
                        break;
                    case 4:
                        $params['procedure5_id'] = $row['id'];
                        $params['procedure5_manhours'] = $row['manhours'];
                        break;
                    case 5:
                        $params['procedure6_id'] = $row['id'];
                        $params['procedure6_manhours'] = $row['manhours'];
                        break;
                    case 6:
                        $params['procedure7_id'] = $row['id'];
                        $params['procedure7_manhours'] = $row['manhours'];
                        break;
                    case 7:
                        $params['procedure8_id'] = $row['id'];
                        $params['procedure8_manhours'] = $row['manhours'];
                        break;
                    case 8:
                        $params['procedure9_id'] = $row['id'];
                        $params['procedure9_manhours'] = $row['manhours'];
                        break;
                    case 9:
                        $params['procedure10_id'] = $row['id'];
                        $params['procedure10_manhours'] = $row['manhours'];
                        break;
                    case 10:
                        $params['procedure11_id'] = $row['id'];
                        $params['procedure11_manhours'] = $row['manhours'];
                        break;
                    case 11:
                        $params['procedure12_id'] = $row['id'];
                        $params['procedure12_manhours'] = $row['manhours'];
                        break;
                }
                $maxnum++;
            }

            $params['maxnum'] = $maxnum;
        }
        return $params;
    }


    //http://fastmegoccr.test/admin/aps/planlog/dopython
    //print a
    public function dopython()
    {

        $output = exec('python test.py');//需放在public目录下
//        echo $output."</br>";

        $array = explode(',', $output);
        foreach ($array as $value) {
            echo $value . "</br>";
        }
        echo "是的发送地方</br>";
    }

    function doaps()
    {
        //所有未完成的工单
        $planlist = collection($this->model->where('finishnum < maxnum')->select())->toArray();
        foreach ($planlist as &$row) {
            $startdate = [];
            $startdate[] = $row['procedure1_startdate'];
            $startdate[] = $row['procedure2_startdate'];
            $startdate[] = $row['procedure3_startdate'];
            $startdate[] = $row['procedure4_startdate'];
            $startdate[] = $row['procedure5_startdate'];
            $startdate[] = $row['procedure6_startdate'];
            $startdate[] = $row['procedure7_startdate'];
            $startdate[] = $row['procedure8_startdate'];
            $startdate[] = $row['procedure9_startdate'];
            $startdate[] = $row['procedure10_startdate'];
            $startdate[] = $row['procedure11_startdate'];
            $startdate[] = $row['procedure12_startdate'];
            $finishdate = [];
            $finishdate[] = $row['procedure1_finishdate'];
            $finishdate[] = $row['procedure2_finishdate'];
            $finishdate[] = $row['procedure3_finishdate'];
            $finishdate[] = $row['procedure4_finishdate'];
            $finishdate[] = $row['procedure5_finishdate'];
            $finishdate[] = $row['procedure6_finishdate'];
            $finishdate[] = $row['procedure7_finishdate'];
            $finishdate[] = $row['procedure8_finishdate'];
            $finishdate[] = $row['procedure9_finishdate'];
            $finishdate[] = $row['procedure10_finishdate'];
            $finishdate[] = $row['procedure11_finishdate'];
            $finishdate[] = $row['procedure12_finishdate'];

            $row['startdate'] = $startdate;
            $row['finishdate'] = $finishdate;

            $man = [];
            $man[] = $row['procedure1_man_id'];
            $man[] = $row['procedure2_man_id'];
            $man[] = $row['procedure3_man_id'];
            $man[] = $row['procedure4_man_id'];
            $man[] = $row['procedure5_man_id'];
            $man[] = $row['procedure6_man_id'];
            $man[] = $row['procedure7_man_id'];
            $man[] = $row['procedure8_man_id'];
            $man[] = $row['procedure9_man_id'];
            $man[] = $row['procedure10_man_id'];
            $man[] = $row['procedure11_man_id'];
            $man[] = $row['procedure12_man_id'];
            $row['man'] = $man;

            $manhours = [];
            $manhours[] = $row['procedure1_manhours'];
            $manhours[] = $row['procedure2_manhours'];
            $manhours[] = $row['procedure3_manhours'];
            $manhours[] = $row['procedure4_manhours'];
            $manhours[] = $row['procedure5_manhours'];
            $manhours[] = $row['procedure6_manhours'];
            $manhours[] = $row['procedure7_manhours'];
            $manhours[] = $row['procedure8_manhours'];
            $manhours[] = $row['procedure9_manhours'];
            $manhours[] = $row['procedure10_manhours'];
            $manhours[] = $row['procedure11_manhours'];
            $manhours[] = $row['procedure12_manhours'];
            $row['manhours'] = $manhours;
        }

    }

    public function znpd()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            //临时表记录插入planlog
            $newlist = collection(\app\admin\model\wu\Planlog0::all())->toArray();
            foreach ($newlist as $params) {
                unset($params['create_time']);
                unset($params['update_time']);
                $row = $this->model->where('workorder_id', $params['workorder_id'])->find();
                if ($row) {
                    $row->allowField(true)->save($params);
                } else {
                    //不可能进来
                    unset($params['id']);
                    $this->model->allowField(true)->save($params);
                }
            }
            $this->success('已使用新排单');
        }

        $output = system('python apsAPI.py');//需放在public目录下

        $row = array();
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    //智能排单结果列表
    public function znpdlist()
    {
        if ($this->request->isAjax()) {
            $zmodel = new \app\admin\model\wu\Znpa();
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $zmodel
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $zmodel
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
    }


    public function gantt_z($ids)
    {
        $list = collection($this->model->where('id', 'in', $ids)->select())->toArray();

        $wolist = [];
        $startdatemin = 0;
        foreach ($list as &$row) {
            $workorder = \app\admin\model\wu\Workorder::get($row['workorder_id']);
            $wolist[] = $workorder['name'] . "\n" . $workorder['ProductName'];

            $startdate = strtotime($row['procedure1_startdate']);
            if ($startdatemin == 0 || $startdatemin > $startdate) {
                $startdatemin = $startdate;
            }
            $row = $this->addArrayinfo($row);
        }

        $startdatemin = date("Y-m-d", $startdatemin);
        $this->view->assign("rawData", $list);
        $this->view->assign("baseTime", strtotime($startdatemin));
        $this->view->assign("wolist", $wolist);
        return $this->view->fetch();
    }

    function addArrayinfo($row)
    {
        $procedure_name = [];
        $procedure_name[] = $this->getprocedure_name($row['procedure1_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure2_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure3_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure4_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure5_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure6_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure7_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure8_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure9_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure10_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure11_id']);
        $procedure_name[] = $this->getprocedure_name($row['procedure12_id']);

        $start_time = [];
        $end_time = [];
        $start_time[] = $row['procedure1_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure1_startdate']);
        $end_time[] = $row['procedure1_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure1_finishdate']);
        $start_time[] = $row['procedure2_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure2_startdate']);
        $end_time[] = $row['procedure2_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure2_finishdate']);
        $start_time[] = $row['procedure3_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure3_startdate']);
        $end_time[] = $row['procedure3_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure3_finishdate']);
        $start_time[] = $row['procedure4_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure4_startdate']);
        $end_time[] = $row['procedure4_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure4_finishdate']);
        $start_time[] = $row['procedure5_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure5_startdate']);
        $end_time[] = $row['procedure5_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure5_finishdate']);
        $start_time[] = $row['procedure6_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure6_startdate']);
        $end_time[] = $row['procedure6_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure6_finishdate']);
        $start_time[] = $row['procedure7_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure7_startdate']);
        $end_time[] = $row['procedure7_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure7_finishdate']);
        $start_time[] = $row['procedure8_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure8_startdate']);
        $end_time[] = $row['procedure8_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure8_finishdate']);
        $start_time[] = $row['procedure9_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure9_startdate']);
        $end_time[] = $row['procedure9_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure9_finishdate']);
        $start_time[] = $row['procedure10_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure10_startdate']);
        $end_time[] = $row['procedure10_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure10_finishdate']);
        $start_time[] = $row['procedure11_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure11_startdate']);
        $end_time[] = $row['procedure11_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure11_finishdate']);
        $start_time[] = $row['procedure12_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure12_startdate']);
        $end_time[] = $row['procedure12_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure12_finishdate']);

        $procedure_man = [];
        $procedure_man[] = $this->getman_name($row['procedure1_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure2_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure3_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure4_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure5_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure6_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure7_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure8_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure9_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure10_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure11_man_id']);
        $procedure_man[] = $this->getman_name($row['procedure12_man_id']);

        $procedure_manid = [];
        $procedure_manid[] = $row['procedure1_man_id'];
        $procedure_manid[] = $row['procedure2_man_id'];
        $procedure_manid[] = $row['procedure3_man_id'];
        $procedure_manid[] = $row['procedure4_man_id'];
        $procedure_manid[] = $row['procedure5_man_id'];
        $procedure_manid[] = $row['procedure6_man_id'];
        $procedure_manid[] = $row['procedure7_man_id'];
        $procedure_manid[] = $row['procedure8_man_id'];
        $procedure_manid[] = $row['procedure9_man_id'];
        $procedure_manid[] = $row['procedure10_man_id'];
        $procedure_manid[] = $row['procedure11_man_id'];
        $procedure_manid[] = $row['procedure12_man_id'];

        $procedures = [];
        $procedures[] = array('id' => $row['procedure1_id'], 'manhours' => $row['procedure1_manhours'], 'mannum' => $row['procedure1_mannum']);
        $procedures[] = array('id' => $row['procedure2_id'], 'manhours' => $row['procedure2_manhours'], 'mannum' => $row['procedure2_mannum']);
        $procedures[] = array('id' => $row['procedure3_id'], 'manhours' => $row['procedure3_manhours'], 'mannum' => $row['procedure3_mannum']);
        $procedures[] = array('id' => $row['procedure4_id'], 'manhours' => $row['procedure4_manhours'], 'mannum' => $row['procedure4_mannum']);
        $procedures[] = array('id' => $row['procedure5_id'], 'manhours' => $row['procedure5_manhours'], 'mannum' => $row['procedure5_mannum']);
        $procedures[] = array('id' => $row['procedure6_id'], 'manhours' => $row['procedure6_manhours'], 'mannum' => $row['procedure6_mannum']);
        $procedures[] = array('id' => $row['procedure7_id'], 'manhours' => $row['procedure7_manhours'], 'mannum' => $row['procedure7_mannum']);
        $procedures[] = array('id' => $row['procedure8_id'], 'manhours' => $row['procedure8_manhours'], 'mannum' => $row['procedure8_mannum']);
        $procedures[] = array('id' => $row['procedure9_id'], 'manhours' => $row['procedure9_manhours'], 'mannum' => $row['procedure9_mannum']);
        $procedures[] = array('id' => $row['procedure10_id'], 'manhours' => $row['procedure10_manhours'], 'mannum' => $row['procedure10_mannum']);
        $procedures[] = array('id' => $row['procedure11_id'], 'manhours' => $row['procedure11_manhours'], 'mannum' => $row['procedure11_mannum']);
        $procedures[] = array('id' => $row['procedure12_id'], 'manhours' => $row['procedure12_manhours'], 'mannum' => $row['procedure12_mannum']);


        $row['procedure_names'] = $procedure_name;
        $row['procedure_mans'] = $procedure_man;
        $row['procedure_manids'] = $procedure_manid;
        $row['start_times'] = $start_time;
        $row['end_times'] = $end_time;
        $row['procedures'] = $procedures;

        return $row;
    }

    //批量重置
    public function plcz($ids)
    {
        $list = $this->model->where('id', 'in', $ids)->select();

        foreach ($list as $row) {
            $row->procedure1_startdate = '0000-00-00 00:00:00';
            $row->procedure1_finishdate = '0000-00-00 00:00:00';
            $row->procedure2_startdate = '0000-00-00 00:00:00';
            $row->procedure2_finishdate = '0000-00-00 00:00:00';
            $row->procedure3_startdate = '0000-00-00 00:00:00';
            $row->procedure3_finishdate = '0000-00-00 00:00:00';
            $row->procedure4_startdate = '0000-00-00 00:00:00';
            $row->procedure4_finishdate = '0000-00-00 00:00:00';
            $row->procedure5_startdate = '0000-00-00 00:00:00';
            $row->procedure5_finishdate = '0000-00-00 00:00:00';
            $row->procedure6_startdate = '0000-00-00 00:00:00';
            $row->procedure6_finishdate = '0000-00-00 00:00:00';
            $row->procedure7_startdate = '0000-00-00 00:00:00';
            $row->procedure7_finishdate = '0000-00-00 00:00:00';
            $row->procedure8_startdate = '0000-00-00 00:00:00';
            $row->procedure8_finishdate = '0000-00-00 00:00:00';
            $row->procedure9_startdate = '0000-00-00 00:00:00';
            $row->procedure9_finishdate = '0000-00-00 00:00:00';
            $row->procedure10_startdate = '0000-00-00 00:00:00';
            $row->procedure10_finishdate = '0000-00-00 00:00:00';
            $row->procedure11_startdate = '0000-00-00 00:00:00';
            $row->procedure11_finishdate = '0000-00-00 00:00:00';
            $row->procedure12_startdate = '0000-00-00 00:00:00';
            $row->procedure12_finishdate = '0000-00-00 00:00:00';

            $row->finishnum = 0;
            $row->save();
        }

        $this->success('批量重置OK');
    }

    public function gstj($ids)
    {
        $params = $this->model->get($ids);
        if (!$params)
            $this->error(__('No Results were found'));

        $procedures = [];
        $procedures[] = array('id' => $params['procedure1_id'], 'manhours' => $params['procedure1_manhours']);
        $procedures[] = array('id' => $params['procedure2_id'], 'manhours' => $params['procedure2_manhours']);
        $procedures[] = array('id' => $params['procedure3_id'], 'manhours' => $params['procedure3_manhours']);
        $procedures[] = array('id' => $params['procedure4_id'], 'manhours' => $params['procedure4_manhours']);
        $procedures[] = array('id' => $params['procedure5_id'], 'manhours' => $params['procedure5_manhours']);
        $procedures[] = array('id' => $params['procedure6_id'], 'manhours' => $params['procedure6_manhours']);
        $procedures[] = array('id' => $params['procedure7_id'], 'manhours' => $params['procedure7_manhours']);
        $procedures[] = array('id' => $params['procedure8_id'], 'manhours' => $params['procedure8_manhours']);
        $procedures[] = array('id' => $params['procedure9_id'], 'manhours' => $params['procedure9_manhours']);
        $procedures[] = array('id' => $params['procedure10_id'], 'manhours' => $params['procedure10_manhours']);
        $procedures[] = array('id' => $params['procedure11_id'], 'manhours' => $params['procedure11_manhours']);
        $procedures[] = array('id' => $params['procedure12_id'], 'manhours' => $params['procedure12_manhours']);

        $workorder = \app\admin\model\wu\Workorder::get($params->workorder_id);
        $mainprocess_id = $workorder->mainprocess_id;

        $sql = "SELECT s.id, s.workorder_id, s.mainprocess_id, s.procedure_id,
MAX(s.procedure_manhours) AS MaxManhours,
ROUND(AVG(s.procedure_manhours),4) AS AVGManhours,
MIN(s.procedure_manhours) AS MinManhours,
PD.name as procedurename,
m.name as mainprocessname
FROM fa_wu_statistics s
left join fa_wu_procedure PD ON PD.id  = s.procedure_id
left join fa_wu_mainprocess m ON m.id  = s.mainprocess_id
left join fa_wu_devicetype t ON t.id  = PD.devicetype_id
WHERE s.procedure_manhours > 0 and s.mainprocess_id = {$mainprocess_id}
GROUP BY s.procedure_id";

        $list = Db::query($sql);

        foreach ($list as &$one) {
            foreach ($procedures as $row) {
                if ($row['id'] == $one['procedure_id']) {
                    $one['manhours'] = $row['manhours'];
                    break;
                }
            }
        }

        $this->view->assign("list", $list);
        $this->view->assign("workordername", $workorder['name']);

        return $this->view->fetch();
    }

    public function gstj0()
    {
        $sql = "SELECT s.mainprocess_id, s.procedure_id,
MAX(s.procedure_manhours) AS MaxManhours,
ROUND(AVG(s.procedure_manhours),4) AS AVGManhours,
MIN(s.procedure_manhours) AS MinManhours,
PD.name as procedurename,
m.name as mainprocessname
FROM fa_wu_statistics s
left join fa_wu_procedure PD ON PD.id  = s.procedure_id
left join fa_wu_mainprocess m ON m.id  = s.mainprocess_id
left join fa_wu_devicetype t ON t.id  = PD.devicetype_id
WHERE s.procedure_manhours > 0 and s.mainprocess_id > 0
GROUP BY mainprocess_id, procedure_id";//执行插入操作

        $list = Db::query($sql);

        $this->view->assign("list", $list);

        return $this->view->fetch();
    }

    public function detail($ids)
    {
        $params = $this->model->get($ids);
        if (!$params)
            $this->error(__('No Results were found'));

        if ($this->request->isAjax()) {
            $params = $params->toArray();
            $params = $this->addArrayinfo($params);

            $procedures = [];
            $procedures[] = array('id' => $params['procedure1_id'], 'manhours' => $params['procedure1_manhours'], 'mannum' => $params['procedure1_mannum']);
            $procedures[] = array('id' => $params['procedure2_id'], 'manhours' => $params['procedure2_manhours'], 'mannum' => $params['procedure2_mannum']);
            $procedures[] = array('id' => $params['procedure3_id'], 'manhours' => $params['procedure3_manhours'], 'mannum' => $params['procedure3_mannum']);
            $procedures[] = array('id' => $params['procedure4_id'], 'manhours' => $params['procedure4_manhours'], 'mannum' => $params['procedure4_mannum']);
            $procedures[] = array('id' => $params['procedure5_id'], 'manhours' => $params['procedure5_manhours'], 'mannum' => $params['procedure5_mannum']);
            $procedures[] = array('id' => $params['procedure6_id'], 'manhours' => $params['procedure6_manhours'], 'mannum' => $params['procedure6_mannum']);
            $procedures[] = array('id' => $params['procedure7_id'], 'manhours' => $params['procedure7_manhours'], 'mannum' => $params['procedure7_mannum']);
            $procedures[] = array('id' => $params['procedure8_id'], 'manhours' => $params['procedure8_manhours'], 'mannum' => $params['procedure8_mannum']);
            $procedures[] = array('id' => $params['procedure9_id'], 'manhours' => $params['procedure9_manhours'], 'mannum' => $params['procedure9_mannum']);
            $procedures[] = array('id' => $params['procedure10_id'], 'manhours' => $params['procedure10_manhours'], 'mannum' => $params['procedure10_mannum']);
            $procedures[] = array('id' => $params['procedure11_id'], 'manhours' => $params['procedure11_manhours'], 'mannum' => $params['procedure11_mannum']);
            $procedures[] = array('id' => $params['procedure12_id'], 'manhours' => $params['procedure12_manhours'], 'mannum' => $params['procedure12_mannum']);

            $list = [];
            for ($i = 0; $i < $params['maxnum']; $i++) {
                $manhours = $procedures[$i]['manhours'];

                $procedure_name = $params['procedure_names'][$i];
                $iskucun = 0;
                if ($procedure_name == '白刀庫存' || $procedure_name == '半成品庫存') {
                    $iskucun = 1;
                }

                $zt = "未完工";
                $procedurestate = \app\admin\model\wu\Procedurestate::where("planlog_id", $params['id'])
                    ->where("procedure_id", $procedures[$i]['id'])->find();
                if ($procedurestate) {
                    $state = intval($procedurestate->state);
                    if ($iskucun) {
                        if ($state == 0) {
                            $zt = "未入庫";
                        } else if ($state == 1) {
                            $zt = "已入庫";
                        } else {
                            $zt = "已出庫";
                        }
                    } else {
                        if ($state == 0) {
                            $zt = "待施工";
                        } else if ($state == 1) {
                            $zt = "工作中";
                        } else {
                            $zt = "完工";
                        }
                    }
                }
                $list[] = array(
                    'id' => $i + 1,
                    'procedure_name' => $params['procedure_names'][$i],
                    'procedure_man_name' => $params['procedure_mans'][$i],
                    'procedure_mannum' => $procedures[$i]['mannum'],
                    'procedure_mannum2' => $procedures[$i]['mannum'],
                    'num0' => $procedurestate['num0'],
                    'num1' => $procedurestate['num1'],
//                    'procedure_preparedate' =>$row['$procedure_name'][$i],
                    'procedure_startdate' => date("Y-m-d H:i:s", $params['start_times'][$i]),
                    'procedure_finishdate' => date("Y-m-d H:i:s", $params['end_times'][$i]),
                    'procedure_manhours' => $manhours,
                    'finish' => $zt,
                );
            }

            $total = count($list);

            $result = array("total" => $total, "rows" => $list);
            return json($result);
        } else {
            $this->assignconfig('ids', $ids);

            Session::set("planlog_ids", $ids);
        }

        //工序甘特图
        $list = collection($this->model->where('id', $ids)->select())->toArray();

        $wolist = [];
        $startdatemin = 0;
        foreach ($list as &$row) {
            $workorder = \app\admin\model\wu\Workorder::get($row['workorder_id']);
            $wolist[] = $workorder['name'] . "\n" . $workorder['ProductName'];

            $startdate = strtotime($row['procedure1_startdate']);
            if ($startdatemin == 0 || $startdatemin > $startdate) {
                $startdatemin = $startdate;
            }
            $row = $this->addArrayinfo($row);
        }

        $startdatemin = date("Y-m-d", $startdatemin);
        $this->view->assign("rawData", $list);
        $this->view->assign("baseTime", strtotime($startdatemin));
        $this->view->assign("wolist", $wolist);

        return $this->view->fetch();
    }

    //点击工单列表的排程
    public function detail2($ids)
    {
        $params = $this->model->where('workorder_id', $ids)->find();

        if (!$params)
            $this->error(__('No Results were found'));

        if ($this->request->isAjax()) {
            $params = $params->toArray();
            $params = $this->addArrayinfo($params);

            $procedures = [];
            $procedures[] = array('id' => $params['procedure1_id'], 'manhours' => $params['procedure1_manhours'], 'mannum' => $params['procedure1_mannum']);
            $procedures[] = array('id' => $params['procedure2_id'], 'manhours' => $params['procedure2_manhours'], 'mannum' => $params['procedure2_mannum']);
            $procedures[] = array('id' => $params['procedure3_id'], 'manhours' => $params['procedure3_manhours'], 'mannum' => $params['procedure3_mannum']);
            $procedures[] = array('id' => $params['procedure4_id'], 'manhours' => $params['procedure4_manhours'], 'mannum' => $params['procedure4_mannum']);
            $procedures[] = array('id' => $params['procedure5_id'], 'manhours' => $params['procedure5_manhours'], 'mannum' => $params['procedure5_mannum']);
            $procedures[] = array('id' => $params['procedure6_id'], 'manhours' => $params['procedure6_manhours'], 'mannum' => $params['procedure6_mannum']);
            $procedures[] = array('id' => $params['procedure7_id'], 'manhours' => $params['procedure7_manhours'], 'mannum' => $params['procedure7_mannum']);
            $procedures[] = array('id' => $params['procedure8_id'], 'manhours' => $params['procedure8_manhours'], 'mannum' => $params['procedure8_mannum']);
            $procedures[] = array('id' => $params['procedure9_id'], 'manhours' => $params['procedure9_manhours'], 'mannum' => $params['procedure9_mannum']);
            $procedures[] = array('id' => $params['procedure10_id'], 'manhours' => $params['procedure10_manhours'], 'mannum' => $params['procedure10_mannum']);
            $procedures[] = array('id' => $params['procedure11_id'], 'manhours' => $params['procedure11_manhours'], 'mannum' => $params['procedure11_mannum']);
            $procedures[] = array('id' => $params['procedure12_id'], 'manhours' => $params['procedure12_manhours'], 'mannum' => $params['procedure12_mannum']);

            for ($i = 0; $i < $params['maxnum']; $i++) {
                $manhours = $procedures[$i]['manhours'];

                $procedure_name = $params['procedure_names'][$i];
                $iskucun = 0;
                if ($procedure_name == '白刀庫存' || $procedure_name == '半成品庫存') {
                    $iskucun = 1;
                }

                $zt = "未完工";
                $procedure_man_name = "";
                $msg = "";//損耗備註
                $procedurestate = \app\admin\model\wu\Procedurestate::where("planlog_id", $params['id'])
                    ->where("procedure_id", $procedures[$i]['id'])->find();
                if ($procedurestate) {
                    $state = intval($procedurestate->state);
                    $man = \app\admin\model\wu\Man::get($procedurestate->man_id);
                    if ($man) {
                        $procedure_man_name = $man->name;
                    }

                    $msg = $procedurestate->msg;
                    if ($iskucun) {
                        if ($state == 0) {
                            $zt = "未入庫";
                        } else if ($state == 1) {
                            $zt = "已入庫";
                        } else {
                            $zt = "已出庫";
                        }
                    } else {
                        if ($state == 0) {
                            $zt = "待施工";
                        } else if ($state == 1) {
                            $zt = "工作中";
                        } else {
                            $zt = "完工";
                        }
                    }
                }

                $list[] = array(
                    'id' => $i + 1,
                    'procedure_name' => $params['procedure_names'][$i],
                    'procedure_man_name' => $procedure_man_name,
                    'procedure_jt' => $params['procedure_mans'][$i],//已是机台名
                    'procedure_mannum' => $procedures[$i]['mannum'],
                    'procedure_mannum2' => $procedures[$i]['mannum'],
                    'num0' => $procedurestate['num0'],
                    'num1' => $procedurestate['num1'],
                    'snum' => $procedurestate['num0'] - $procedurestate['num1'],
                    'msg' => $msg,
//                    'procedure_preparedate' =>$row['$procedure_name'][$i],
                    'procedure_startdate' => date("Y-m-d H:i:s", $params['start_times'][$i]),
                    'procedure_finishdate' => date("Y-m-d H:i:s", $params['end_times'][$i]),
                    'procedure_manhours' => $manhours,
                    'finish' => $zt,
                );
            }

            $total = count($list);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        } else {
            $this->assignconfig('ids', $params->id);
            $this->assignconfig('workorder_id', $ids);

            Session::set("planlog_ids", $params->id);
        }

        //工序甘特图
        $list = collection($this->model->where('id', $params->id)->select())->toArray();

        $wolist = [];
        $startdatemin = 0;
        foreach ($list as &$row) {
            $workorder = \app\admin\model\wu\Workorder::get($row['workorder_id']);
            $wolist[] = $workorder['name'] . "\n" . $workorder['ProductName'];

            $startdate = strtotime($row['procedure1_startdate']);
            if ($startdatemin == 0 || $startdatemin > $startdate) {
                $startdatemin = $startdate;
            }
            $row = $this->addArrayinfo($row);
        }

        $startdatemin = date("Y-m-d", $startdatemin);
        $this->view->assign("rawData", $list);
        $this->view->assign("baseTime", strtotime($startdatemin));
        $this->view->assign("wolist", $wolist);
        $this->view->assign("title", "製令單工作詳情");

        return $this->view->fetch('');
    }

    public function detail_forworkorder($ids)
    {
        $params = $this->model->get($ids);
        if (!$params)
            $this->error(__('No Results were found'));

        if ($this->request->isAjax()) {
            $params = $params->toArray();

            $model1 = new \app\admin\model\wu\Workorder;
            $list = $model1
                ->with(['productspec'])
                ->where('workorder.id', $params['workorder_id'])
                ->limit(1)
                ->select();

            $total = count($list);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
    }

    const APS_PLANLOG_SWITCH = "__switch__";

    public function pcak()
    {

        $msg = "排程全开";
        if ($this->switch == 0) {
            $this->switch = 1;
        } else {
            $this->switch = 0;
            $msg = "排程全关";
        }

        Session::set(self::APS_PLANLOG_SWITCH, $this->switch);
        $sql = "UPDATE fa_wu_planlog SET switch = " . $this->switch;
        Db::execute($sql);
        $this->success($msg, null, ['switch' => $this->switch]);

    }

    public function editpdday()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $vk = \app\admin\model\wu\Vk::get(1);
                $vk->v = $params['v'];
                $vk->save();
                $this->success('排单开始日修改成功');
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $pdday = date("Y-m-d", time());
        $vk = \app\admin\model\wu\Vk::get(1);
        if ($vk) {
            $pdday = $vk->v;
        }
        $row = [];
        $row['v'] = $pdday;
        $this->view->assign("row", $row);

        return $this->view->fetch();
    }


    const APS_PLANLOG_XCKBLISTPAGE = "__pxckbpage__";

    public function xckb()
    {

        $now = date('Y-m-d');
        $tomorrow = date("Y-m-d", strtotime("+1 day"));
        $tomorrowA = strtotime($tomorrow);
        $nowA = strtotime($now);

        $where = "maxnum > finishnum and switch = 1 and (
                (Date(procedure1_startdate) <= '{$now}' and Date(procedure1_finishdate) >= '{$now}')
                or (Date(procedure2_startdate) <= '{$now}' and Date(procedure2_finishdate) >= '{$now}')
                or (Date(procedure3_startdate) <= '{$now}' and Date(procedure3_finishdate) >= '{$now}')
                or (Date(procedure4_startdate) <= '{$now}' and Date(procedure4_finishdate) >= '{$now}')
                or (Date(procedure5_startdate) <= '{$now}' and Date(procedure5_finishdate) >= '{$now}')
                or (Date(procedure6_startdate) <= '{$now}' and Date(procedure6_finishdate) >= '{$now}')
                or (Date(procedure7_startdate) <= '{$now}' and Date(procedure7_finishdate) >= '{$now}')
                or (Date(procedure8_startdate) <= '{$now}' and Date(procedure8_finishdate) >= '{$now}')
                or (Date(procedure9_startdate) <= '{$now}' and Date(procedure9_finishdate) >= '{$now}')
                or (Date(procedure10_startdate) <= '{$now}' and Date(procedure10_finishdate) >= '{$now}')
                or (Date(procedure11_startdate) <= '{$now}' and Date(procedure11_finishdate) >= '{$now}')
                or (Date(procedure12_startdate) <= '{$now}' and Date(procedure12_finishdate) >= '{$now}')
            )";


        $list = $this->model
            ->with(['workorder'])
            ->where($where)
            ->where('finishnum < maxnum')
            ->select();

        $list = collection($list)->toArray();

        $list2 = []; //今日需要做的制程

        foreach ($list as $row) {
            $row = $this->addArrayinfo($row);

            $start = $row['finishnum'];
            $end = $row['maxnum'];
            for ($i = 0; $i < $end; $i++) {
                if ($row['start_times'][$i] > $tomorrowA || $row['end_times'][$i] < $nowA) {
                    continue;
                }

                $zt = "未完工";
                if ($i < $start) {
                    $zt = "已完工";
                }

                $list2[] = array(
                    'man_name' => $row['procedure_mans'][$i],
                    'procedure_name' => $row['procedure_names'][$i],
                    'num' => $row['workorder']['num'],
                    'man_num' => $row['procedures'][$i]['mannum'],
                    'startdate' => date("Y-m-d H:i", $row['start_times'][$i]),
                    'workorder_name' => $row['workorder']['name'],
                    'manhours' => $row['procedures'][$i]['manhours'],
                    'MainProcessName' => $row['workorder']['ProductName'],
                    'ProductNumber' => $row['workorder']['ProductName'],
                    'ProductName' => $row['workorder']['ProductName'],
                    'workorder_id' => $row['workorder_id'],
                    'zt' => $zt,
                );
            }
        }

        $list3 = [];

        $num = count($list2);
        $pagemax = ceil($num / 10) - 1;

        $page = 0;//每页显示10个; 从第0页开始
        $page = Session::get(self::APS_PLANLOG_XCKBLISTPAGE);
        if (is_null($page)) {
            $page = 0;
        } else {
            $page = intval($page);
            $page++;
            if ($page > $pagemax) {
                $page = 0;
            }
        }
        Session::set(self::APS_PLANLOG_XCKBLISTPAGE, $page);
        $i = 0;
        $start = $page * 10;
        $end = $start + 9;
        foreach ($list2 as $row) {
            if ($i >= $start && $i <= $end) {
                $list3[] = $row;
            }
            $i++;
        }

        $this->view->assign("list", $list3);
        $page++;
        $pagemax++;
        $this->view->assign("pageinfo", "第" . $page . "页 / 共" . $pagemax . "页");

        return $this->view->fetch();
    }


    /**
     * 批量更新
     */
    public function multi($ids = "")
    {
        $ids = $ids ? $ids : $this->request->param("ids");
        if ($ids) {
            if ($this->request->has('params')) {
                parse_str($this->request->post("params"), $values);
//                if (!$this->auth->isSuperAdmin()) {
//                    $values = array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
//                }
                if ($values) {
                    $adminIds = $this->getDataLimitAdminIds();
                    if (is_array($adminIds)) {
                        $this->model->where($this->dataLimitField, 'in', $adminIds);
                    }
                    $count = 0;
                    $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
                    foreach ($list as $index => $item) {
                        $count += $item->allowField(true)->isUpdate(true)->save($values);
                    }
                    if ($count) {
                        $this->success();
                    } else {
                        $this->error(__('No rows were updated'));
                    }
                } else {
                    $this->error(__('You have no permission'));
                }
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    public function ygyg()
    {
        $xdata = [];
        $ydata = [];

        $list = \app\admin\model\wu\Ygyg::all();
        if ($list) {
            $list = collection($list)->toArray();
//            $list = array_reverse($list);
            foreach ($list as $row) {
                $xdata[] = $row['date'];
                $ydata[] = $row['gsb'];
            }
        }

        $mannum = \app\admin\model\wu\Man::where('state', 1)->count();

        $this->view->assign("xdata", $xdata);
        $this->view->assign("ydata", $ydata);
        $this->view->assign("mannum", $mannum);

        return $this->view->fetch();
    }

    public function tzrl()
    {
        if ($this->request->isAjax()) {
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);

            $offset = $this->request->get("offset", 0);
            $limit = $this->request->get("limit", 0);

            $where = "maxnum > finishnum and switch = 1 and (
                (procedure1_mannum > 1)
                or (procedure2_mannum > 1)
                or (procedure3_mannum > 1)
                or (procedure4_mannum > 1)
                or (procedure5_mannum > 1)
                or (procedure6_mannum > 1)
                or (procedure7_mannum > 1)
                or (procedure8_mannum > 1)
                or (procedure9_mannum > 1)
                or (procedure10_mannum > 1)
                or (procedure11_mannum > 1)
                or (procedure12_mannum > 1)
            )";

            $list = $this->model
                ->with(['workorder'])
                ->where($where)
                ->where('finishnum < maxnum')
                ->select();

            $list = collection($list)->toArray();
            $list2 = []; //今日需要做的制程
            $total = 0;
            foreach ($list as $row) {
                $row = $this->addArrayinfo($row);

                $end = $row['maxnum'];
                for ($i = 0; $i < $end; $i++) {

                    if ($total >= $offset && $total < $offset + $limit) {

                        $list2[] = array(
                            'workorder' => $row['workorder'],
                            'man_name' => $row['procedure_mans'][$i],
                            'procedure_name' => $row['procedure_names'][$i],
                            'num' => $row['workorder']['num'],
                            'man_num' => $row['procedures'][$i]['mannum'],
                            'man_num2' => $row['procedures'][$i]['mannum'],
                            'startdate' => date("Y-m-d H:i", $row['start_times'][$i]),
                            'workorder_name' => $row['workorder']['name'],
                            'manhours' => $row['procedures'][$i]['manhours'],
                            'manhours2' => $row['procedures'][$i]['manhours'],
                            'MainProcessName' => $row['workorder']['MainProcessName'],
                            'ProductNumber' => $row['workorder']['ProductNumber'],
                            'ProductName' => $row['workorder']['ProductName'],
                            'workorder_id' => $row['workorder_id'],
                        );
                    }
                    $total++;
                }
            }

            $result = array("total" => $total, "rows" => $list2);

            return json($result);
        }

        return $this->view->fetch();
    }


    public function daka($ids = NULL)
    {
        $row = \app\admin\model\wu\Procedurestate::get($ids);
        if (!$row)
            $this->error(__('No Results were found'));

        $planlog = \app\admin\model\wu\Planlog::get($row->planlog_id);
        if ($planlog) {
            $workorder = \app\admin\model\wu\Workorder::get($planlog->workorder_id);
        }

        $procedure = \app\admin\model\wu\Procedure::get($row->procedure_id);

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    $procedurestatelog = new \app\admin\model\wu\Procedurestatelog();
                    $procedurestatelog->procedurestate_id = $ids;
                    $procedurestatelog->man_id = $params['man_id'];
                    $procedurestatelog->msg  = $params['msg'];
                    $procedurestatelog->save();

                    if (isset($params['lbnumber']) && !empty($params['lbnumber']) && $planlog && $workorder) {
                        $workorder->lbnumber = $params['lbnumber'];
                        $workorder->save();
                    }
                    if (intval($row->state) == 0) {
                        $params['state'] = 1;
                        $params['startdate'] = date("Y-m-d H:i:s", time());
                    } else {
                        $params['state'] = 2;
                        $params['enddate'] = date("Y-m-d H:i:s", time());
                    }

                    $result = $row->allowField(true)->save($params);
                    $this->success();
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $daka = '開工';
        if (intval($row->state) > 0) {
            $daka = '完工';
        }
        $this->view->assign("daka", $daka);
        $this->view->assign("state", $row->state);
        $this->view->assign("workorder", $workorder);
        $this->view->assign("procedure", $procedure);
        //白刀工单， 取出断差，即在半成品庫存工序时，输入工单的雷標編號
        //转拨单和成品工单
        $inputlb = 0;
        if ($procedure->name == '白刀庫存' || $procedure->name == '半成品庫存') {
            $inputlb = 1;
        }

        $this->view->assign("inputlb", $inputlb);

        return $this->view->fetch();
    }

    public function detail3($ids)
    {
//        $params = $this->model->where('workorder_id', $ids)->find();
        $params = $this->model->get($ids);

        if (!$params)
            $this->error(__('No Results were found'));

        if ($this->request->isAjax()) {
            $params = $params->toArray();
            $params = $this->addArrayinfo($params);

            $procedures = [];
            $procedures[] = array('id' => $params['procedure1_id'], 'manhours' => $params['procedure1_manhours'], 'mannum' => $params['procedure1_mannum']);
            $procedures[] = array('id' => $params['procedure2_id'], 'manhours' => $params['procedure2_manhours'], 'mannum' => $params['procedure2_mannum']);
            $procedures[] = array('id' => $params['procedure3_id'], 'manhours' => $params['procedure3_manhours'], 'mannum' => $params['procedure3_mannum']);
            $procedures[] = array('id' => $params['procedure4_id'], 'manhours' => $params['procedure4_manhours'], 'mannum' => $params['procedure4_mannum']);
            $procedures[] = array('id' => $params['procedure5_id'], 'manhours' => $params['procedure5_manhours'], 'mannum' => $params['procedure5_mannum']);
            $procedures[] = array('id' => $params['procedure6_id'], 'manhours' => $params['procedure6_manhours'], 'mannum' => $params['procedure6_mannum']);
            $procedures[] = array('id' => $params['procedure7_id'], 'manhours' => $params['procedure7_manhours'], 'mannum' => $params['procedure7_mannum']);
            $procedures[] = array('id' => $params['procedure8_id'], 'manhours' => $params['procedure8_manhours'], 'mannum' => $params['procedure8_mannum']);
            $procedures[] = array('id' => $params['procedure9_id'], 'manhours' => $params['procedure9_manhours'], 'mannum' => $params['procedure9_mannum']);
            $procedures[] = array('id' => $params['procedure10_id'], 'manhours' => $params['procedure10_manhours'], 'mannum' => $params['procedure10_mannum']);
            $procedures[] = array('id' => $params['procedure11_id'], 'manhours' => $params['procedure11_manhours'], 'mannum' => $params['procedure11_mannum']);
            $procedures[] = array('id' => $params['procedure12_id'], 'manhours' => $params['procedure12_manhours'], 'mannum' => $params['procedure12_mannum']);

            for ($i = 0; $i < $params['maxnum']; $i++) {
                $manhours = $procedures[$i]['manhours'];

                $procedure_name = $params['procedure_names'][$i];
                $iskucun = 0;
                if ($procedure_name == '白刀庫存' || $procedure_name == '半成品庫存') {
                    $iskucun = 1;
                }

                $num1 = 0;//数量
                $num2 = 0;//損耗
                $zt = "未完工";
                $procedurestate = \app\admin\model\wu\Procedurestate::where("planlog_id", $params['id'])
                    ->where("procedure_id", $procedures[$i]['id'])->find();
                if ($procedurestate) {
                    $state = intval($procedurestate->state);
                    $num1 = $procedurestate->num0;
                    $num2 = $procedurestate->num0 - $procedurestate->num1;
                    if ($iskucun) {
                        if ($state == 0) {
                            $zt = "未入庫";
                        } else if ($state == 1) {
                            $zt = "已入庫";
                        } else {
                            $zt = "已出庫";
                        }
                    } else {
                        if ($state == 0) {
                            $zt = "待施工";
                        } else if ($state == 1) {
                            $zt = "工作中";
                        } else {
                            $zt = "完工";
                        }
                    }
                }

                $list[] = array(
                    'id' => $i + 1,
                    'procedure_name' => $params['procedure_names'][$i],
                    'procedure_man_name' => $params['procedure_mans'][$i],
                    'procedure_mannum' => $procedures[$i]['mannum'],
                    'procedure_mannum2' => $procedures[$i]['mannum'],
                    'num1' => $num1,
                    'num2' => $num2,
//                    'procedure_preparedate' =>$row['$procedure_name'][$i],
                    'procedure_startdate' => date("Y-m-d H:i:s", $params['start_times'][$i]),
                    'procedure_finishdate' => date("Y-m-d H:i:s", $params['end_times'][$i]),
                    'procedure_manhours' => $manhours,
                    'finish' => $zt,
                );
            }

            $total = count($list);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        } else {
            $this->assignconfig('ids', $params->id);

            Session::set("planlog_ids", $params->id);
        }

        //工序甘特图
        $list = collection($this->model->where('id', $params->id)->select())->toArray();

        $wolist = [];
        $startdatemin = 0;
        foreach ($list as &$row) {
            $workorder = \app\admin\model\wu\Workorder::get($row['workorder_id']);
            $wolist[] = $workorder['name'] . "\n" . $workorder['ProductName'];

            $startdate = strtotime($row['procedure1_startdate']);
            if ($startdatemin == 0 || $startdatemin > $startdate) {
                $startdatemin = $startdate;
            }
            $row = $this->addArrayinfo($row);
        }

        $startdatemin = date("Y-m-d", $startdatemin);
        $this->view->assign("rawData", $list);
        $this->view->assign("baseTime", strtotime($startdatemin));
        $this->view->assign("wolist", $wolist);


        $model1 = new \app\admin\model\wu\Workorder;
        $workorder = $model1
            ->with(['productspec'])
            ->where('workorder.id', $params['workorder_id'])
            ->limit(1)
            ->find();
        $this->view->assign("workorder", $workorder);

        return $this->view->fetch('');
    }


}
